package com.sky.module.system.datatrace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.module.system.datatrace.dao.DataTraceMapper;
import com.sky.module.system.datatrace.domain.entity.DataTracerEntity;
import com.sky.module.system.datatrace.domain.form.DataTraceQueryForm;
import com.sky.module.system.datatrace.service.DataTraceService;
import com.sky.module.system.user.domain.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author
 * @date 2023/3/6 22:36
 **/
@Service
public class DataTraceServiceImpl implements DataTraceService {

    @Autowired
    private DataTraceMapper dataTraceMapper;

    @Override
    public void addTrace(DataTracerEntity dataTracerEntity) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
//        dataTracerEntity.set
    }

    /***
     * @description 分页查询
     * @param dataTraceQueryForm
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.sky.module.system.datatrace.domain.entity.DataTracerEntity>
     * @date 2023/3/9 0:31
     *
     */
    @Override
    public Page<DataTracerEntity> queryPage(DataTraceQueryForm dataTraceQueryForm) {
        LambdaQueryWrapper<DataTracerEntity> wrapper = new LambdaQueryWrapper<>();
        Page<DataTracerEntity> page = new Page<>(dataTraceQueryForm.getPageNum(), dataTraceQueryForm.getPageSize());
        wrapper.like(StringUtils.isNotBlank(dataTraceQueryForm.getUsername()), DataTracerEntity::getUserName, dataTraceQueryForm.getUsername());
//         大于起始时间
        wrapper.ge(StringUtils.isNotBlank(dataTraceQueryForm.getStartTime()), DataTracerEntity::getCreateTime, dataTraceQueryForm.getStartTime());
//          小于结束时间
        wrapper.le(StringUtils.isNotBlank(dataTraceQueryForm.getEndTime()), DataTracerEntity::getCreateTime, dataTraceQueryForm.getEndTime());
        final Page<DataTracerEntity> pageResult = dataTraceMapper.selectPage(page, wrapper);
        return pageResult;
    }

    @Override
    public boolean deleteById(Integer id) {
        return dataTraceMapper.deleteById(id) > 0 ? true : false;
    }

    public static void main(String[] args) {
//                new Thread(()->{
//
//                })
    }
}
